﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PhatPhucLoc.dao;
using PhatPhucLoc.model;
using PhatPhucLoc.common;

namespace PhatPhucLoc.view
{
    public partial class formThemKhachHang : Form
    {
        private FormKhachHang formKhachHang = null;
        private KhachHangModel khachHangModel;

        public formThemKhachHang(KhachHangModel _khachHangModel, ref FormKhachHang _formKhachHang)
        {
            InitializeComponent();
            FormChiTietKhachHangLoad();
            formKhachHang = _formKhachHang;

            // load information khach mua hang
            khachHangModel = _khachHangModel;
            txtDiaChi.Text = khachHangModel.DiaChi;
            txtDienThoai.Text = khachHangModel.Tel;
            txtEmail.Text = khachHangModel.Email;
            txtFax.Text = khachHangModel.Fax;
            txtNganHang.Text = khachHangModel.NganHang;
            txtHoTen.Text = khachHangModel.Ten;
            txtMaSoThue.Text = khachHangModel.MaSoThue;
            txtTaiKhoan.Text = khachHangModel.SoTaiKhoan;
            txtGhiChu.Text = khachHangModel.GhiChu;
        }

        public formThemKhachHang(ref FormKhachHang _formKhachHang)
        {
            InitializeComponent();
            FormChiTietKhachHangLoad();
            formKhachHang = _formKhachHang;
        }


        public void FormChiTietKhachHangLoad()
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietKhachHang_KeyDown);
        }

        private void FormChiTietKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text.Equals(""))
            {
                MessageBox.Show("Họ Tên Khách Hàng Không Được Trống");
                txtHoTen.Focus();
            }
            else
            {
                if (khachHangModel == null)
                {
                    khachHangModel = new KhachHangModel();
                }
                khachHangModel.GhiChu = txtGhiChu.Text;
                khachHangModel.DiaChi = txtDiaChi.Text;
                khachHangModel.Email = txtEmail.Text;
                khachHangModel.Fax = txtFax.Text;
                khachHangModel.MaSoThue = txtMaSoThue.Text;
                khachHangModel.SoTaiKhoan = txtTaiKhoan.Text;
                khachHangModel.Tel = txtDienThoai.Text;
                khachHangModel.Ten = txtHoTen.Text;
                khachHangModel.NganHang = txtNganHang.Text;

                // new
                if (khachHangModel.Id == null || khachHangModel.Id == "")
                {
                    if (KhachHangDAO.Insert(khachHangModel))
                    {
                        MessageBox.Show("Thêm khách hàng thành công");
                        if (formKhachHang != null)
                        {
                            formKhachHang.FormKhachMuaHangLoad();
                        }
                        this.Dispose();
                    }
                }
                // update
                else
                {
                    if (KhachHangDAO.Update(khachHangModel))
                    {
                        MessageBox.Show("Cập nhật khách hàng thành công");
                        formKhachHang.FormKhachMuaHangLoad();
                        this.Dispose();
                    }
                }
            }
        }



        private void btnKetThuc_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
